<template>
  <el-form :model="role">
    <el-form-item label="用户名称" :label-width="formLabelWidth">
      <el-input v-model="role.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="用户编码" :label-width="formLabelWidth">
      <el-input v-model="role.code" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="描述" :label-width="formLabelWidth">
      <el-input type="textarea" v-model="role.description" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addUser, getRoleById } from '@/services/role'

export default {
  name: 'addUser',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: Number
    }
  },
  data () {
    return {
      role: {
        name: '',
        code: '',
        description: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    async onSubmit () {
      const { data } = await addUser(this.role)
      if (data.code === '000000') {
        this.$message.success('添加成功')
        this.$emit('success')
        this.role = {}
      }
    },
    onCancel () {
      this.$emit('cancel')
    },
    async loadRoleInfo () {
      const { data } = await getRoleById(this.roleId)
      if (data.code === '000000') {
        this.role = data.data
        console.log(this.role)
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.loadRoleInfo()
    }
  }

}
</script>

<style>

</style>
